<?php
/**
 * Created by PhpStorm.
 * User: Edward
 * Date: 2019/5/31
 * Time: 13:23
 */

namespace app\api\controller\config;
use app\admin\model\ChinaArea;
use app\admin\model\ChinaCity;
use app\admin\model\ChinaProvince;
use app\api\model\Area;
use app\lib\exception\NotFoundException;
use app\Validate\IdMustPositive;
use think\Controller;
use think\Request;

class Address extends Controller {

    /**
     * 获取省份信息
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function province(){
        $province = ChinaProvince::province()->toArray();
//        $province = (new ChinaProvince())->selectList([]);
        if(empty($province)){
            throw new NotFoundException([
                'msg'=>'暂无省份信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['province'=>$province]);
    }
    /**
     *  根据省份ID查寻下级城市
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function city(Request $request){
        (new IdMustPositive())->goCheck();
        $provinceId = $request->id;
        $city = ChinaCity::city($provinceId)->toArray();
        if(empty($city)){
            throw  new NotFoundException([
                'msg'=>'暂无城市信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['city'=>$city]);
    }
    /**
     * 根据城市ID查寻下级地区
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function area(Request $request){
        (new IdMustPositive())->goCheck();
        $cityId = $request->id;
        $area = ChinaArea::area($cityId)->toArray();
        if(empty($area)){
            throw  new NotFoundException([
                'msg'=>'暂无地区信息，请联系管理员'
            ]);
        }
        return writeJson('200',['area'=>$area]);
    }

    /**
     * 获取数据库地图三级联动
     */
    public function getAreaList(){
        $areaService = new Area();
        $list = $areaService->select()->toArray();
        $result = $this->list_to_tree($list,'id','parent_id','children');
        return writeJson("200",$result);
    }
    /**
     * 获取省份信息
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function provinceN(){
        $areaService = new Area();
        $where["rank"] = 0;
        $list = $areaService->where($where)->select()->toArray();
        if(empty($list)){
            throw new NotFoundException([
                'msg'=>'暂无省份信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['province'=>$list]);
    }
    /**
     *  根据省份ID查寻下级城市
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cityN(Request $request){
        (new IdMustPositive())->goCheck();
        $provinceId = $request->id;
        $areaService = new Area();
        $where["rank"] = 1;
        $where["parent_id"] = $provinceId;
        $list = $areaService->where($where)->select()->toArray();
        if(empty($list)){
            throw  new NotFoundException([
                'msg'=>'暂无城市信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['city'=>$list]);
    }
    /**
     * 根据城市ID查寻下级地区
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function areaN(Request $request){
        (new IdMustPositive())->goCheck();
        $cityId = $request->id;
        $areaService = new Area();
        $where["rank"] = 2;
        $where["parent_id"] = $cityId;
        $list = $areaService->where($where)->select()->toArray();
        if(empty($list)){
            throw  new NotFoundException([
                'msg'=>'暂无地区信息，请联系管理员'
            ]);
        }
        return writeJson('200',['area'=>$list]);
    }

    /**
     * 造树
     * @param $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     */
    private function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
}